import apiClient from "@/api/api";
import store from "@/store";

export const checkUserAuth = async () => {
  const token = localStorage.getItem('token')
  if (token) {
    try {
      const response = await apiClient.get('/user/checkLogin')
      const userData = response.data
      if (userData.code === 1) {
        // 更新store中的用户状态
        store.commit('setUserIdentity', userData.data.identity)
        store.commit('setUserId', userData.data.id)
        return true
      } else {
        clearUserData()
        return false
      }
    } catch (error) {
      clearUserData()
      return false
    }
  }
  return false
}

export const clearUserData = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userId')
  localStorage.removeItem('userRole')
} 